// @algorithm @lc id=100286 lang=cpp
// @title he-bing-liang-ge-pai-xu-de-lian-biao-lcof

#include "algm/algm.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        ListNode *newHead = new ListNode(-1);
        ListNode *temp = newHead;
        if (l1 == nullptr && l2 == nullptr) {
            return nullptr;
        }
        while (l1 != nullptr && l2 != nullptr) {
            if (l1->val <= l2->val) {
                temp->next = l1;
                l1 = l1->next;
                temp = temp->next;
            }
            else {
                temp->next = l2;
                l2 = l2->next;
                temp = temp->next;
            }
        }
        if (l1 != nullptr) {
            temp->next = l1;
        }
        if (l2 != nullptr) {
            temp->next = l2;
        }
        return newHead->next;
    }
};